import requests
import json
'''
"https://kyfw.12306.cn/otn/leftTicket/
query?leftTicketDTO.train_date=2017-06-27
&leftTicketDTO.from_station=XKS
&leftTicketDTO.to_station=LYS
&purpose_codes=ADULT"
'''
class Seat():
    def __init__(self):
        self.SoftSleep=''
        self.HardSleep=''
        self.NoSeat=''
        self.FriSeat=''
        self.SecSeat=''
        self.HardSeat=''
        self.Business=''
        

class Train():
    def __init__(self):
        self.Name=''
        self.Start=''
        self.End=''
        self.From=''
        self.To=''
        self.FromTime=''
        self.ToTime=''
        self.CostTime=''
        self.Date=''
        self.Seat=Seat()


class QuerySpider():
    def __init__(self,date,FromID,ToID,TicketType='ADULT'):

        self.date=date
        self.FromID=FromID
        self.ToID=ToID
        self.TicketType=TicketType
        self.headers={'user-agent':r'Mozilla/14.0 (Windows NT 16.0; Win64; x128 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.2987.133 Safari/537.36'}
        self.QueryUrl="https://kyfw.12306.cn/otn/leftTicket/queryZ"
        self.demand={'leftTicketDTO.train_date':self.date,
                        'leftTicketDTO.from_station':self.FromID,
                        'leftTicketDTO.to_station':self.ToID,
                        'purpose_codes':self.TicketType
        }

        

    def query(self):
        self.Session=requests.Session()
        self.Session.headers=self.headers
        #visit the init page to get cookies
        self.Session.get('https://kyfw.12306.cn/otn/queryTrainInfo/init')
        
        r=self.Session.get(self.QueryUrl,params=self.demand)
        r.encoding='utf-8'
        try:
            ResultList=json.loads(r.text)['data']['result']
        except KeyError as keyerror:
            print(keyerror)
            print(f"[debug] url:{r.url}")
            print(f"[debug] response:{r.text}")

        self.TrainList=[]
        
#        print("日期: ",date,"  ",FromID," 到 ",ToID)
        for res in ResultList:
            train=Train()
            InfoList=res.split('|')
            train.Name=InfoList[3]
            train.Start=InfoList[4]
            train.End=InfoList[5]
            train.From=InfoList[6]
            train.To=InfoList[7]
            train.FromTime=InfoList[8]
            train.ToTime=InfoList[9]
            train.CostTime=InfoList[10]
            train.Date=InfoList[12]
            train.Seat.FriSeat=InfoList[31]
            train.Seat.SecSeat=InfoList[30]
            train.Seat.NoSeat=InfoList[26]
            self.TrainList.append(train)
            
            # if (InfoList[6]==FromID and InfoList[7]==ToID):
            
                # print("车次: {}   始发: {}   终到: {}   发车时间: {}   预计到达时间: {}".format(InfoList[3],InfoList[4],InfoList[5],InfoList[8],InfoList[9]))
                # print("一等座: {}   二等座: {}   无座: {}".format(InfoList[31],InfoList[30],InfoList[26],))
                # print(InfoList)
                # print()
                
